package com.fqgj.sentry.policy.dao.impl;

import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.sentry.policy.dao.PcPolicyDao;
import com.fqgj.sentry.policy.entity.PcPolicyEntity;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: yujianfu
 * Date: 2017-07-12
 * Time: 09:37:58.131
 */

@Repository
public class PcPolicyDaoImpl extends AbstractBaseMapper<PcPolicyEntity> implements PcPolicyDao {

    @Override
    public List<PcPolicyEntity> selectPolicyByExecutorId(Long executorId) {
        return getSqlSession().selectList(getStatement("selectPolicyByExecutorId"), executorId);
    }

    @Override
    public List<PcPolicyEntity> selectListByPolicySetId(String policySetId) {
        return getSqlSession().selectList(getStatement("selectListByPolicySetId"), policySetId);
    }

    @Override
    public List<PcPolicyEntity> selectPolicyByPolicySetId(Long policySetId) {
        return getSqlSession().selectList(getStatement("selectPolicyByPolicySetId"), policySetId);
    }

    @Override
    public List<PcPolicyEntity> selectByPolicySetIdAndExecutorId(Long policySetId, Long executorId) {
        Map<String, Object> map = new HashMap<>();
        map.put("policySetId", policySetId);
        map.put("executorId", executorId);
        return getSqlSession().selectList(getStatement("selectByPolicySetIdAndExecutorId"), map);
    }

    @Override
    public Boolean updatePolicySort(Long policyId, Integer sort) {
        Map<String, Object> map = new HashMap<>();
        map.put("policyId", policyId);
        map.put("sort", sort);
        return getSqlSession().update(getStatement("updatePolicySort"), map) >= 1;
    }

    @Override
    public Integer selectMaxCode(Long policySetsId) {
        PcPolicyEntity policyEntity = getSqlSession().selectOne(getStatement("selectMaxCode"), policySetsId);
        if (policyEntity != null && policyEntity.getCode() != null) {
            return Integer.valueOf(policyEntity.getCode());
        }
        return null;
    }

    @Override
    public Integer selectMaxSort(Long policySetsId) {
        PcPolicyEntity policyEntity = getSqlSession().selectOne(getStatement("selectMaxSort"), policySetsId);
        if (policyEntity != null && policyEntity.getSort() != null) {
            return policyEntity.getSort();
        }
        return null;
    }

    @Override
    public void deleteByPolicySetId(Long policySetId) {
        Map<String, Object> map = new HashMap<>();
        map.put("policySetsId", policySetId);
        getSqlSession().selectList(getStatement("deleteByPolicySetId"), map);
    }
}